package com.pathfinder.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.pathfinder.GameClient;
import com.pathfinder.R;
import com.pathfinder.model.Game;
import com.pathfinder.model.Player;
import com.pathfinder.providers.GamesListProvider;
import com.pathfinder.providers.HttpGamesListProvider;
import com.pathfinder.utils.Function;

import java.util.List;

import static com.pathfinder.RequestSender.sendJoinGameRequest;
import static com.pathfinder.utils.CollectionUtils.mapList;

/**
 * Date: 08.05.11
 *
 * @author Victor Passichenko (vpas@yandex-team.ru)
 */
public class StartGameActivity extends Activity {

    private GamesListProvider gamesListProvider;

    public StartGameActivity() {
        super();
        gamesListProvider = new HttpGamesListProvider();
    }

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start_game);

        loadGameListAndSetOnItemClickListener();
    }

    public void onShowCreateGameActivityButtonClick(View view) {
        String playerName = ((EditText) findViewById(R.id.PlayerNameEditText)).getText().toString();

        if (playerName != null && playerName.length() > 0) {
            Player player = new Player(playerName);
            GameClient.setPlayer(player);
            Intent intent = new Intent(this, CreateGameActivity.class);
            startActivity(intent);
        } else {
            Toast.makeText(this, "Enter your name!", Toast.LENGTH_LONG).show();
        }
    }

    private void loadGameListAndSetOnItemClickListener() {
        ListView listView = (ListView) findViewById(R.id.GameListView);
        final List<Game> games = gamesListProvider.getAvailableGames();
        List<String> values = mapList(GAME_TO_STRING, games);

        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Game game = games.get(position);
                joinGame(game);
            }
        });
    }

    private void joinGame(Game game) {
        String playerName = ((EditText) findViewById(R.id.PlayerNameEditText)).getText().toString();

        if (playerName != null && playerName.length() > 0) {
            Player player = new Player(playerName);
            if (sendJoinGameRequest(game, player)) {
                GameClient.setGame(game);
                GameClient.setPlayer(player);
                Intent intent = new Intent(this, GameLobbyActivity.class);
                startActivity(intent);
            } else {
                Toast.makeText(this, "This name has been already taken, choose another!", Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(this, "Enter your name!", Toast.LENGTH_LONG).show();
        }
    }

    private static Function<Game, String> GAME_TO_STRING = new Function<Game, String>() {
        public String apply(Game arg) {
            return arg.getName();
        }
    };
}
